package agents;

import env.Action;
import env.Enviroment;
import env.Tile;

/**
 * @author user
 */
public class GreedyAgent extends Agent {

    Tile target = null;

    public GreedyAgent(String name, int row, int col) {
        super(name, row, col);
    }

    @Override
    public Action getMove() {
        if(target==null)
            findTarget();
        if(target==null)
            return null;
        final AgentState state = getState();
        final int row=state.getRow(), col=state.getCol(), tRow=target.getRow(), tCol=target.getCol();
        if(tRow < row && state.isLegalMove(Action.up))
            return Action.up;
        if(tRow > row && state.isLegalMove(Action.down))
            return Action.down;
        if(tCol < col && state.isLegalMove(Action.left))
            return Action.left;
        if(tCol > col && state.isLegalMove(Action.right))
            return Action.right;
        target=null;
        return null;
    }
    //sets this.target to the closest flag. finds 
    private void findTarget() {
        int d = Integer.MAX_VALUE;
        //int row=getRow(), col=getCol();
        Tile current = getState().getTile();
        for (Tile t : Enviroment.env.flags) {
            int temp = current.manhattan(t);
            if (temp < d) {
                d = temp;
                target = t;
            }
        }
    }
}
